﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml.Serialization;

namespace zEngine
{
    public class Profiling
    {
        private Game _game;
        private SpriteBatch _spriteBatch;

        public int Fps = 0; // Frame per seconds
        double LastCheckFps = 0;
        int FpsCounter = 0;

        public int Ups = 0; // Update per second
        double LastCheckUps = 0;
        int UpsCounter = 0;

        private bool _drawOnscreen = false;
        private SpriteFont _font;
        private Color _textColor = Color.White;

        #region Properties
        public Game Game
        {
            get { return _game; }
            set { _game = value; }
        }
        public SpriteBatch spriteBatch
        {
            get { return _spriteBatch; }
            set { _spriteBatch = value; }
        }
        public SpriteFont Font
        {
            get { return _font; }
            set { _font = value; }
        }
        public bool DrawOnscreen
        {
            get { return _drawOnscreen; }
            set { _drawOnscreen = value; }
        }
        public Color TextColor
        {
            get { return _textColor; }
            set { _textColor = value; }
        }
        #endregion

        /// <summary>
        /// Initializes a new instance of the Profiling class.
        /// </summary>
        public Profiling(Game game, SpriteBatch sb)
        {
            _game = game;
            _spriteBatch = sb;
        }

        public void Update(GameTime gameTime)
        {
            countUps(gameTime);
        }

        public void Draw(GameTime gameTime)
        {
            countFps(gameTime);
            if (_drawOnscreen && _font != null)
            {
                _spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.SaveState);
                _spriteBatch.DrawString(_font, "UPS : " + Ups.ToString(), new Vector2(40, 20), _textColor);
                _spriteBatch.DrawString(_font, "FPS : " + Fps.ToString(), new Vector2(40, 35), _textColor);
                _spriteBatch.End();
            }
        }

        void countFps(GameTime gameTime)
        {
            double currentMs = gameTime.TotalRealTime.TotalMilliseconds;
            FpsCounter++;
            if (currentMs - LastCheckFps > 1000)
            {
                // If more then 1 sec from the last check, store how many frame drawed
                Fps = FpsCounter;

                // Reset counter value
                FpsCounter = 0;
                LastCheckFps = currentMs;
            }
        }

        void countUps(GameTime gameTime)
        {
            double currentMs = gameTime.TotalRealTime.TotalMilliseconds;
            UpsCounter++;
            if (currentMs - LastCheckUps > 1000)
            {
                // If more then 1 sec from the last check, store how many frame drawed
                Ups = UpsCounter;

                // Reset counter value
                UpsCounter = 0;
                LastCheckUps = currentMs;
            }
        }
    }
}
